package com.rao.controller;


import com.rao.core.excel.entity.StudentExcel;
import com.rao.core.excel.parse.ParseExcelUtil;
import com.rao.entity.Student;
import com.rao.entity.VO.CommonResponseVO;
import com.rao.mapper.StudentMapper;
import com.rao.mideng.annotation.RaoLogAnno;
import com.rao.mideng.annotation.RaoMiDeng;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 学生表 前端控制器
 * </p>
 *
 * @author rao3030
 * @since 2021-11-04
 */
@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    SqlSessionFactory sqlSessionFactory;

    @ApiOperation("从Excel文件导入学生到数据库里面去")
    @RaoLogAnno
//    @RaoMiDeng//fastjson针对HttpServletRequest参数报错
    @RequestMapping(path = "/importStudentByExcel", method = RequestMethod.POST)
    public CommonResponseVO importStudentByExcel(HttpServletRequest request, @RequestParam(name = "file") MultipartFile file) throws IOException {
        //自动提交关闭+批处理器
        if (!file.isEmpty()) {
            //将上传的文件保存到目标文件夹里面去
            String descPath = System.getProperty("user.dir") + File.separator + "src/main/resources/excel/upload";
            String newFileName = descPath + File.separator + UUID.randomUUID() + file.getOriginalFilename();
            File newFile = new File(newFileName);
            file.transferTo(newFile);

            //解析excel文件
            List<StudentExcel> studentExcels = new LinkedList<>();
            ParseExcelUtil.parseExcel2Sheet(newFile, studentExcels);

            //导入数据库
            SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
            StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
            studentExcels.forEach(stu -> {
                Student student = new Student();
                //Student要与StudentExcel 属性名且类型一致
                BeanUtils.copyProperties(stu, student);
                studentMapper.insert(student);
            });
//            sqlSession.rollback();
            sqlSession.commit();
            sqlSession.close();
        }
        return CommonResponseVO.success("文件导入succeed");
    }


    @ApiOperation("导出查询的信息到excel文件去")
    @RaoLogAnno
    @RaoMiDeng
    @RequestMapping(path = "/downloadFile", method = RequestMethod.POST)
    public CommonResponseVO downloadFile(@RequestParam(name = "file") MultipartFile file, HttpRequest request) {
        return null;
    }
}
